//
//  AppDelegate.m
//  weibo
//
//  Created by qiuming on 15/12/21.
//  Copyright © 2015年 qiuming. All rights reserved.
//

#import "AppDelegate.h"
#import <AFNetworkActivityIndicatorManager.h>
#import <CocoaLumberjack.h>
#import "MainViewController.h"
#import "VisitorViewController.h"
#import "UserAccountModel.h"
#import "NewFeatureController.h"
#import "WelcomeViewController.h"
#import "OAuthViewController.h"

/// 切换控制器通知
NSString *const SwitchRootViewControllerNotification = @"SwitchRootViewControllerNotification";

@interface AppDelegate ()
/// 监听对象
@property (strong, nonatomic) id observer;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 设置DDLog打印
    [self setDDlog];
    
    // 设置网络
    [self setupNetWork];
    // 设置显示
    [self setupAppearance];
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.window.rootViewController = [self defultRootViewController];
    
    [self.window makeKeyAndVisible];
    
    // 利用通知切换控制器
    __weak typeof (self) weakSelf = self;
    self.observer = [[NSNotificationCenter defaultCenter]addObserverForName:SwitchRootViewControllerNotification object:nil queue:nil usingBlock:^(NSNotification * note) {
        // 是否经OAuth认证
        NSString *className = ([note.object isKindOfClass:[OAuthViewController class]]) ? @"WelcomeViewController" : @"MainViewController";
        Class cls = NSClassFromString(className);
        
        weakSelf.window.rootViewController = [[cls alloc]init];
    }];
    
    return YES;
}

/// 登录逻辑判断
- (UIViewController *)defultRootViewController {
    if ([UserAccountViewModel sharedUserAccount].isLogin) {
        
        return [self isNewVersion] ? [[NewFeatureController alloc]init] : [[WelcomeViewController alloc]init];
    } else {
        return [[MainViewController alloc]init];
    }
}

/// 是否是新版本
- (BOOL)isNewVersion {
    // 取出当前版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    // 取出沙盒版本号
    NSString *sandboxVersionKey = @"sandboxVersionKey";
    NSUserDefaults *userDefault =  [NSUserDefaults standardUserDefaults];
    NSString *sandboxVersion = [userDefault stringForKey:sandboxVersionKey];
    
    // 保存当前版本号
    [userDefault setObject:currentVersion forKey:sandboxVersionKey];
    
    return [currentVersion compare:sandboxVersion] == NSOrderedDescending;
}

/// 2.设置显示
- (void)setupAppearance {
    [UINavigationBar appearance].tintColor = [UIColor orangeColor];
    [UITabBar appearance].tintColor = [UIColor orangeColor];
}

/// 1.设置内存缓存、磁盘缓存大小
- (void)setupNetWork {

    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    
    NSURLCache *cache = [[NSURLCache alloc]initWithMemoryCapacity:10 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
    
    [NSURLCache setSharedURLCache:cache];
    
}

- (void)setDDlog {
    
    // 设置 DDLog
    setenv("XcodeColors", "YES", 0);
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    
    [[DDTTYLogger sharedInstance]setColorsEnabled:YES];
    [[DDTTYLogger sharedInstance]setForegroundColor:[UIColor orangeColor] backgroundColor:nil forFlag:DDLogFlagInfo];
    [[DDTTYLogger sharedInstance]setForegroundColor:[UIColor yellowColor] backgroundColor:nil forFlag:DDLogFlagVerbose];
}


@end
